#####################################################################
# Cardinality Matching Representative 
#####################################################################

rm(list=ls())

sink("~/Dropbox/Geographic Natural Experiments/02_analyses/04_match1000m.txt")

library(designmatch)
library(stargazer)
library(foreign)
library(gurobi)
library(Hmisc)

##############################################
# Read data and prepare basic output
##############################################

d = read.dta("~/Dropbox/Geographic Natural Experiments/01_data/raw_data/Final-Data.dta")
#d_original = d
names(d)

##############################################
# Prepare data
##############################################

# treatment
table(d$treat)
d$t_ind = 1 - d$treat
table(d$t_ind)

# sort by treatment 
d = d[order(d$t_ind, decreasing=TRUE), ]

# check
table(d$buffer_band)
describe(d$hprice_mean)
describe(d$turnout04)
describe(d$turnout06)
describe(d$male)
d$age = 100-d$birth_year
describe(d$age)

# subset buffer
#d = d[d$buffer_band <= 1000,]

##############################################
# Match
##############################################

# Cardinality matching finds the largest matched sample of pairs that meets balance
# requirements. Here the balance requirements are mean balance, fine balance and
# ks balance for different covariates. The solver used is gurobi 6.5.0.

# Treatment indicator
t_ind = d$t_ind

# Moment balance: constrain differences in means to be at most 0.1 standard deviations apart
mom_covs = cbind(d$hprice_mean,d$turnout04,d$turnout06,d$male,d$age)
mom_targets = c(214546,0.638,0.478,0.491,35.3)
mom_tols = absstddif(mom_covs, t_ind, .05)
mom = list(covs = mom_covs, tols = mom_tols, targets = mom_targets)

# Solver options
t_max = 60*30
solver = "gurobi"
approximate = 0
solver = list(name = solver, t_max = t_max, approximate = approximate,
              round_cplex = 0, trace = 1)

# Match
out_1 = cardmatch(t_ind, mom = mom, solver = solver)

# Indices of the treated units and matched controls
t_id = out_1$t_id
c_id = out_1$c_id

table(table(t_id))
table(table(c_id))

# Mean balance
cbind(meantab(mom_covs, t_ind, t_id, c_id, digits=3)[, 4:5], round(mom_targets,3))

# Save matched sample 
d_match_country = d[c(t_id, c_id), ]

# Invert treatment
d_match_country$t_ind = 1 - d_match_country$t_ind

# Save samples 
save(d_match_country, file="~/Dropbox/Geographic Natural Experiments/02_analyses/04_match_country_1000m.RData")

sink()

